package com.zrt.dao.impl;

import com.zrt.dao.ParseDao;
import com.zrt.domain.Author;
import com.zrt.domain.Citation;

import com.zrt.domain.Reference;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Repository
public class ParseDaoImpl implements ParseDao {
    @Override
    public String parseFile(String input) {
        String content = null;
        try {
            // 是否排序
            boolean sort = false;
            // 开始提取页数
            int startPage = 1;
            // 结束提取页数
            int endPage = Integer.MAX_VALUE;

            PDDocument document = PDDocument.load(new File(input));
            PDFTextStripper pts = new PDFTextStripper();
            endPage = document.getNumberOfPages();
            System.out.println("Total Page: " + endPage);
            pts.setStartPage(startPage);
            pts.setEndPage(endPage);
            try {
                //content就是从pdf中解析出来的文本
                content = pts.getText(document);

            }catch (Exception e){
                throw e;
            }finally {
                if (null != document)
                    document.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }

    public String parseDetailForReference(String content){
        String string = null;
        String[] references = null;
        //从 references 开始解析
        if (content.contains("References")){
            references = content.split("References");
            string = references[1].toString();
        }else if (content.contains("references")){
            references = content.split("references");
            string = references[1].toString();
        }else if (content.contains("REFERENCE")){
            references = content.split("REFERENCES");
            string = references[1].toString();
        }

        return string;
    }

    public String parseDetailForAuthor(String content){
        String string = null;
        String[] author = null;
        //从 Abstract 开始解析
        if (content.contains("Abstract")){
            author = content.split("Abstract");
            string = author[0].toString();
        }else if (content.contains("abstract")){
            author = content.split("abstract");
            string = author[0].toString();
        }else if (content.contains("ABSTRACT")){
            author = content.split("ABSTRACT");
            string = author[0].toString();
        }
        String str2 =string.replaceAll("(\\r\\n|\\n|\\\\n|)", "");
        return str2;
    }

    @Override
    public List<Citation> sortingCitation(String string) {
        List<Citation> citationList = new ArrayList<>();

        //去除换行
        String str2 =string.replaceAll("(\\r\\n|\\n|\\\\n|)", "");
        //Pattern p= Pattern.compile("(?:[A-z][a-z]{1,}-)?[A-Z][a-z]{1,}\\s(?:[A-Z]\\s)?[A-Z][a-z]{1,}.*?\\d{4}");
        Pattern p = Pattern.compile("(?:[A-z][a-z]{1,}-)?[A-Z](?:[a-z]{1,})?(?:.)?\\s(?:[A-Z]\\s)?[A-Z][a-z]{1,}.*?\\d{4}");
        Matcher m=p.matcher(str2);

        int index = 1;
        while (m.find()){
            m.start();
            m.end();
            String group = m.group();
            Citation citation = new Citation(0,group);
            citationList.add(citation);
            index++;
        }

        return citationList;
    }

    @Override
    public List<Author> sortingAuthor(String string) {
        List<Author> authorList = new ArrayList<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long times = System.currentTimeMillis();

        Author author = new Author(format.format(times),string);
        authorList.add(author);

        return authorList;
    }

}
